<?php
require_once("include/auth.php");
require_once("include/db.php");
header("Content-Type: application/json");

$device = get_device($_GET['token']);
$device_id = $device['ID'];

if ($_GET['date_1'] == '') $start_date = null;
else $start_date = $_GET['date_1'];
if ($_GET['date_2'] == '') $end_date = null;
else $end_date = $_GET['date_2'];
$result = query_data($start_date, $end_date, $device_id);

$row = mysql_fetch_array($result);
if ($row === false) {
    //无返回结果
    $data['check'] = 'Not Found';
    echo json_encode($data);
    exit();
}

$array_name = array();
$count = 0;
while($row) {
    //时间戳*1000
    $time = strtotime($row['Created_Time']) * 1000;
    //array_data[[时间戳，数据],...]
    if (!(in_array($row['Sensor_Name'], $array_name))) {
        $array_name[] = $row['Sensor_Name'];
        $count++;
    }
    $array_count[$row['Sensor_Name']]++;
    $array_data[$row['Sensor_Name']][] = (double)$row['Value'];
    $array_time[$row['Sensor_Name']][] = $time;
    $array_unit[$row['Sensor_Name']][] = $row['Unit'];
    $array_type[$row['Sensor_Name']][] = $row['Sensor_Type'];

    $row = mysql_fetch_array($result);
}

$data['check'] = 'Success';
$data['count'] = $count;
$data['array_count'] = $array_count;
$data['array_name'] = $array_name;
$data['array_data'] = $array_data;
$data['array_time'] = $array_time;
$data['array_type'] = $array_type;
$data['array_unit'] = $array_unit;
echo json_encode($data);
